package com.videoplayer;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.adapter.BookItemAdapter;
import com.adapter.PopupAdapter;
import com.adapter.YoukuVideoListAdapter;
import com.application.VideosList;
import com.db.DBConnect;
import com.entity.YoukuVideo;
import com.html.VideoListResolve;
import com.html.YoukuJsoup;
import com.umeng.analytics.MobclickAgent;
import com.util.ConfirmDialog;
import com.util.MyToast;
import com.util.NetType;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class SampleListFragment extends Fragment implements OnClickListener {
	Button showLeft;
	private final String DB_GET_WIFI = "name = 'wifi_only'";
	public Document doc;
	// public Document docSearch;
	public Document doc2;
	public YoukuVideo youkuList;
	private VideoListResolve videoAnsy;
	private TextView tvStatus;
	// public List<YoukuVideo> videoList;
	public ListView lvVideo;
	private Button btnSetting;
	private RelativeLayout lyTopBar;
	public YoukuVideoListAdapter lvAdapter;
	public EditText etSearch;
	public InputMethodManager imm;
	private LinearLayout loadingView;
	private Button btnSearch;
	public ImageView ivCover;
	private BookItemAdapter adapter;
	private VideosList videoList;
	private boolean threadFlag;
	public MyTask myTask;
	private PopupWindow listPopWindow;
	private PopupAdapter popAdapter;
	private DBConnect dbCon;
	private int dbWifOnly;
	private ConfirmDialog dialog;
	private int pages;
	private String etSearchText;
	private int currentPage;
	private boolean isSearch;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mView = inflater.inflate(R.layout.main_activity, null);
		showLeft = (Button) mView.findViewById(R.id.showLeft);
		videoList = (VideosList) getActivity().getApplication()
				.getApplicationContext();
		dbCon = new DBConnect(getActivity());
		dbCon.open();
		dbWifOnly = dbCon.queryOneData(DB_GET_WIFI);
		dbCon.close();
		// videoList = new ArrayList<YoukuVideo>();
		lvVideo = (ListView) mView.findViewById(R.id.lv_video);
		lyTopBar = (RelativeLayout) mView.findViewById(R.id.ly_topbar);
		lvVideo.setFocusable(true);
		tvStatus = (TextView) mView.findViewById(R.id.tv_status);
		btnSetting = (Button) mView.findViewById(R.id.btn_setting);
		etSearch = (EditText) mView.findViewById(R.id.et_search);
		btnSearch = (Button) mView.findViewById(R.id.btn_search);
		ivCover = (ImageView) mView.findViewById(R.id.ly_whole);
		btnSearch.setOnClickListener(this);
		ivCover.setOnClickListener(this);
		tvStatus.setOnClickListener(this);
		btnSetting.setOnClickListener(this);
		ivCover.setVisibility(View.GONE);
		videoAnsy = new VideoListResolve(getActivity());

		imm = (InputMethodManager) getActivity().getSystemService(
				getActivity().INPUT_METHOD_SERVICE);
		if (dbWifOnly == 1 && NetType.getAPNType(getActivity()) == 2) {
			dialog = new ConfirmDialog(getActivity(), R.style.MyDialog,
					"检测您当前为非Wifi网络，本应用会消耗大量流量，如要继续，请到设置界面关闭\"只在Wifi下使用\"选项", 0);
			dialog.show();
			Button btnOk = (Button) dialog
					.findViewById(R.id.dialog_confirm_button_ok);
			Button btnCancel = (Button) dialog
					.findViewById(R.id.dialog_confirm_button_cancel);
			btnOk.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setClass(getActivity(), SettingActivity.class);
					startActivityForResult(intent, 0);
				}
			});
			btnCancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					getActivity().finish();
				}
			});
		} else {
			myTask = new MyTask();
			myTask.execute("http://www.youku.com");
		}
		return mView;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 0 && resultCode == -1) {
			dialog.dismiss();
			myTask = new MyTask();
			myTask.execute("http://www.youku.com");
		}
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		// lvVideo.setAdapter(adapter);
		showLeft.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				((MainActivity) getActivity()).showLeft();
			}
		});

	}

	@SuppressLint("NewApi")
	public void initPopupWindow() {
		ListView listView;
		View contentView = View.inflate(getActivity(), R.layout.popup_layout,
				null);
		// contentView.setBackgroundColor(0x99000000);
		listPopWindow = new PopupWindow(getActivity());
		int w = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				140, getActivity().getResources().getDisplayMetrics()));
		int h = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
				350, getActivity().getResources().getDisplayMetrics()));
		listPopWindow.setWidth(w);
		listPopWindow.setHeight(h);
		listPopWindow.setContentView(contentView);
		listPopWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
		listPopWindow.setOutsideTouchable(true);
		String[] name = videoAnsy.getVideoTitle();
		tvStatus.setBackgroundResource(R.drawable.banner_pressed);
		listView = (ListView) contentView.findViewById(R.id.lv_pop);
		listView.setCacheColorHint(0);
		listView.setOnItemClickListener(popListView);
		popAdapter = new PopupAdapter(getActivity(), name);
		listPopWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				tvStatus.setBackgroundResource(R.drawable.banner_default);
			}
		});
		listView.setAdapter(popAdapter);
		// listView.setScrollBarStyle(View.SCROLLBAR_POSITION_DEFAULT);
		listPopWindow.setFocusable(true);
		popAdapter.setFlag(videoAnsy.getCurrentTitle());
		popAdapter.notifyDataSetChanged();

		// 显示PopupWindow
		// listPopWindow.showAsDropDown(contentView,100,(int)(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
		// 55, getActivity().getResources().getDisplayMetrics())));
		listPopWindow.showAsDropDown(
				contentView,
				showLeft.getWidth()
						+ (int) (TypedValue.applyDimension(
								TypedValue.COMPLEX_UNIT_DIP, 14, getActivity()
										.getResources().getDisplayMetrics())),
				lyTopBar.getHeight()
						+ (int) (TypedValue.applyDimension(
								TypedValue.COMPLEX_UNIT_DIP, 23, getActivity()
										.getResources().getDisplayMetrics())));
	}

	OnItemClickListener popListView = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			String[] name = videoAnsy.getVideoTitleUrl();
			videoAnsy.setCurrentTitle(videoAnsy.getVideoTitle()[arg2]);
			AsyncChange(name[arg2]);

			if (listPopWindow != null) {
				listPopWindow.dismiss();
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_search:
			etSearchText = etSearch.getText().toString();
			new MySearchTask().execute(etSearchText);
			break;
		case R.id.ly_whole:
			((MainActivity) getActivity()).showLeft();
			break;
		case R.id.tv_status:
			initPopupWindow();
			break;
		case R.id.btn_setting:
			Intent intent = new Intent();
			intent.setClass(getActivity(), SettingActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	// loaing thread
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				tvStatus.setText("loading   ");
				break;
			case 2:
				tvStatus.setText("loading.  ");
				break;
			case 3:
				tvStatus.setText("loading.. ");
				break;
			case 4:
				tvStatus.setText("loading...");
				break;
			case 0:
				tvStatus.setText(videoAnsy.getCurrentTitle());
				break;
			}
		}
	};

	private void playAnimation() {
		new Thread() {
			@Override
			public void run() {
				while (threadFlag) {
					for (int i = 1; i < 5; i++) {
						mHandler.sendEmptyMessage(i);
						try {
							sleep(300);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
				if (!threadFlag) {
					mHandler.sendEmptyMessage(0);
				}
			}

		}.start();
	}

	/***/

	// get youku search video list
	private String getSearchVideoList(String searchContent) {
		// List<YoukuVideo> videoList = new ArrayList<YoukuVideo>();
		pages = 0;
		videoList.getVideoList().clear();

		try {

			doc = Jsoup
					.connect(
							"http://www.soku.com/search_video/q_"
									+ URLEncoder.encode(searchContent, "UTF-8"))
					.timeout(60000).get();
			// youkuList = new YoukuVideo();
			Elements content = doc.getElementsByClass("v");
			// System.out.println("222222222222222222222" + doc.html());
			Elements content2 = doc.getElementsByClass("pager");
			Elements content3 = content2.get(0).getElementsByTag("li");
			for (Element link2 : content3) {
				Log.v("pages", "%%%%%%%%%%%%%%%%%%" + link2.html());
				pages++;
			}
			pages = pages - 1;
			Log.v("pages", "%%%%%%%%%%%%%%%%%%" + pages);
			for (Element link : content) {
				Elements linkHref = link.getElementsByClass("v_title");
				Elements linkImg = link.getElementsByClass("v_thumb");
				Elements linkClarity = link.getElementsByClass("v_ishd");
				Elements linkTime = link.getElementsByClass("v_time");
				String href = "";
				String vThumb = "";
				String lImg = "";
				String vClarity = "";
				String vTime = "";
				for (Element link3 : linkImg) {
					if ("".equals(link3.getElementsByTag("img").attr("_src"))) {
						lImg = link3.getElementsByTag("img").attr("src");
					} else {
						lImg = link3.getElementsByTag("img").attr("_src");
					}
				}
				for (Element linkclarity : linkClarity) {
					vClarity = linkclarity.children().attr("title");
				}
				for (Element linktime : linkTime) {
					vTime = linktime.children().text();
				}
				for (Element link2 : linkHref) {
					href = link2.children().attr("href");
					vThumb = link2.children().attr("title");
					if (href.contains("v.youku.com/v_show/id")) {
						youkuList = new YoukuVideo(href, vThumb, lImg, vTime,
								vClarity);
						if (!videoList.getVideoList().contains(youkuList)) {
							videoList.getVideoList().add(youkuList);
							// Log.v("youkuList", href + "------" + vThumb
							// + "--------" + lImg);

						}
					}
					break;

					// System.out.println(vThumb);
					// System.out.println(href);

				}

				// String linkText = link.text();

			}
			/**
			 * youkuList = new YoukuVideo(youku.getFocusVideoURL(doc), "头条新闻",
			 * ""); videoList.add(0,youkuList);
			 */
			doc = null;
			return "success";

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "failed";
		}

	}

	private String getSearchVideoListOrderAndPage(String searchContent,
			String order, String page) {
		// List<YoukuVideo> videoList = new ArrayList<YoukuVideo>();
		// videoList.getVideoList().clear();
		try {

			doc = Jsoup
					.connect(
							"http://www.soku.com/search_video/q_"
									+ URLEncoder.encode(searchContent, "UTF-8")
									+ "_orderby_" + order + "_page_" + page)
					.timeout(60000).get();
			Log.v("search", "@@@@@@" + "http://www.soku.com/search_video/q_"
					+ URLEncoder.encode(searchContent, "UTF-8") + "_orderby_"
					+ order + "_page_" + page);
			// youkuList = new YoukuVideo();
			Elements content = doc.getElementsByClass("v");
			// System.out.println("222222222222222222222" + doc.html());

			for (Element link : content) {
				Elements linkHref = link.getElementsByClass("v_title");
				Elements linkImg = link.getElementsByClass("v_thumb");
				Elements linkClarity = link.getElementsByClass("v_ishd");
				Elements linkTime = link.getElementsByClass("v_time");
				String href = "";
				String vThumb = "";
				String lImg = "";
				String vClarity = "";
				String vTime = "";
				for (Element link3 : linkImg) {
					if ("".equals(link3.getElementsByTag("img").attr("_src"))) {
						lImg = link3.getElementsByTag("img").attr("src");
					} else {
						lImg = link3.getElementsByTag("img").attr("_src");
					}
				}
				for (Element linkclarity : linkClarity) {
					vClarity = linkclarity.children().attr("title");
				}
				for (Element linktime : linkTime) {
					vTime = linktime.children().text();
				}
				for (Element link2 : linkHref) {
					href = link2.children().attr("href");
					vThumb = link2.children().attr("title");
					if (href.contains("v.youku.com/v_show/id")) {
						youkuList = new YoukuVideo(href, vThumb, lImg, vTime,
								vClarity);
						if (!videoList.getVideoList().contains(youkuList)) {
							videoList.getVideoList().add(youkuList);
							Log.v("youkuList", href + "------" + vThumb
									+ "--------" + lImg);

						}
					}
					break;

					// System.out.println(vThumb);
					// System.out.println(href);

				}

				// String linkText = link.text();

			}
			/**
			 * youkuList = new YoukuVideo(youku.getFocusVideoURL(doc), "头条新闻",
			 * ""); videoList.add(0,youkuList);
			 */
			doc = null;
			return "success";

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "failed";
		}

	}

	// get the real video url
	@TargetApi(8)
	private Bundle getPlayUrl(String url) {
		String finaUrl = url.replace(url, "http://www.flvcd.com/parse.php?kw="
				+ url);
		String[] finalUrl;
		Bundle b = new Bundle();
		try {
			doc2 = Jsoup.connect(finaUrl).timeout(60000).get();
			// youkuList = new YoukuVideo();
			Elements content = doc2.select("td");
			int length = getListSize(content);
			finalUrl = new String[length];

			for (Element link : content) {
				if (link.children().select("a").hasAttr("onclick")) {
					for (int j = 0; j < link.select("a").toArray().length; j++) {
						String videoUrl = link.select("a").toArray()[j]
								.toString();
						finalUrl[j] = videoUrl.substring(
								videoUrl.indexOf("http:"),
								videoUrl.lastIndexOf("target") - 2);
					}
					b.putCharSequenceArray("href", finalUrl);
					Log.v("flvcd", finalUrl[0]);
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}

	public void AsyncChange(String url) {
		if ("history".equals(url)) {
			isSearch = false;
			MobclickAgent.onEvent(getActivity(), "hitory");
			new HistoryTask().execute(url);

		} else if ("search".equals(url)) {
			isSearch = true;
			MobclickAgent.onEvent(getActivity(), "search");
			showSearchEdit();
		} else {
			new MyTask().execute(url);
		}
	}

	// 异步加载
	public class HistoryTask extends AsyncTask<String, Integer, String> {
		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			etSearch.setVisibility(View.GONE);
			btnSearch.setVisibility(View.GONE);
			showLeft.setVisibility(View.VISIBLE);
			btnSetting.setVisibility(View.VISIBLE);
			tvStatus.setVisibility(View.VISIBLE);
			lvVideo.setVisibility(View.INVISIBLE);
			imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
			videoList.getVideoList().clear();
			tvStatus.setText("loading...");

		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@Override
		protected String doInBackground(String... params) {
			dbCon.open();
			videoList.setHistoryList(dbCon.queryPlayHistoryData());
			dbCon.close();
			return "";
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {

			lvVideo.setVisibility(View.VISIBLE);
			tvStatus.setText("播放记录");
			tvStatus.setOnClickListener(null);
			if (videoList.getHistoryList() != null) {
				adapter = new BookItemAdapter(getActivity(), lvVideo,
						videoList.getHistoryList());
				lvVideo.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				lvVideo.setCacheColorHint(0);
				lvVideo.setSelection(0);
				lvVideo.requestFocus();
			}
			threadFlag = false;
			if (!isSearch) {
				lvVideo.removeFooterView(loadingView);
			}
			lvVideo.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					if (!"".equals(videoList.getHistoryList().get(arg2)
							.getvLink())) {
						Intent intent = new Intent();
						intent.setClass(getActivity(),
								VideoDetailActivity.class);
						intent.putExtra("url",
								videoList.getHistoryList().get(arg2).getvLink());
						intent.putExtra("img",
								videoList.getHistoryList().get(arg2).getvImg());
						intent.putExtra("title", videoList.getHistoryList()
								.get(arg2).getvTitle());
						// intent.setClass(getActivity(),
						// testVideoPlayer.class);
						// intent.putExtra("url", temp_url);
						// intent.putExtra("title",
						// videoList.getVideoList().get(arg2)
						// .getvTitle());
						startActivity(intent);
					} else {
						MyToast toast = new MyToast(getActivity(), "该视频无法为您播放");
						toast.show();
						// Toast.makeText(getActivity(), "该视频无法播放", 1000)
						// .show();
					}
				}
			});

			Log.e("opopop",
					"==================" + videoAnsy.getCurrentContent());
		}

	}

	// 异步加载
	public class MyTask extends AsyncTask<String, Integer, String> {
		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			etSearch.setVisibility(View.GONE);
			btnSearch.setVisibility(View.GONE);
			tvStatus.setVisibility(View.VISIBLE);
			showLeft.setVisibility(View.VISIBLE);
			btnSetting.setVisibility(View.VISIBLE);
			lvVideo.setVisibility(View.INVISIBLE);
			imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
			videoList.getVideoList().clear();
			tvStatus.setText("loading...");
			tvStatus.setOnClickListener(SampleListFragment.this);

		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@Override
		protected String doInBackground(String... params) {
			threadFlag = true;
			playAnimation();
			return videoAnsy.getVideoList(params[0]);
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			// tvStatus.setVisibility(View.INVISIBLE);

			lvVideo.setVisibility(View.VISIBLE);
			// lvAdapter = new YoukuVideoListAdapter(getActivity(), videoList,
			// lvVideo);
			// lvVideo.setAdapter(lvAdapter);

			adapter = new BookItemAdapter(getActivity(), lvVideo,
					videoList.getVideoList());
			lvVideo.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			lvVideo.setCacheColorHint(0);
			lvVideo.setSelection(0);
			lvVideo.requestFocus();
			threadFlag = false;

			lvVideo.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					if (arg2 < videoList.getVideoList().size()) {
						// TODO Auto-generated method stub
						Log.v("vlink", "^^^^^^^^^^^^>>"
								+ videoList.getVideoList().get(arg2).getvLink());
						// Bundle temp_url =
						// getPlayUrl(videoList.getVideoList().get(arg2)
						// .getvLink());
						// if (!"Bundle[{}]".equals(temp_url.toString())) {
						// System.out.println("-------------->"
						// + temp_url.toString());
						if (!"".equals(videoList.getVideoList().get(arg2)
								.getvLink())) {
							Intent intent = new Intent();
							intent.setClass(getActivity(),
									VideoDetailActivity.class);
							intent.putExtra("url", videoList.getVideoList()
									.get(arg2).getvLink());
							intent.putExtra("img", videoList.getVideoList()
									.get(arg2).getvImg());
							intent.putExtra("title", videoList.getVideoList()
									.get(arg2).getvTitle());
							// intent.setClass(getActivity(),
							// testVideoPlayer.class);
							// intent.putExtra("url", temp_url);
							// intent.putExtra("title",
							// videoList.getVideoList().get(arg2)
							// .getvTitle());
							startActivity(intent);
						} else {
							MyToast toast = new MyToast(getActivity(),
									"该视频无法为您播放");
							toast.show();
							// Toast.makeText(getActivity(), "该视频无法播放", 1000)
							// .show();
						}
					} else {
						Toast.makeText(getActivity(), "aaaaaa", 2000).show();
					}
				}
			});

			Log.e("opopop",
					"==================" + videoAnsy.getCurrentContent());
		}

	}

	private void showSearchEdit() {
		etSearch.setVisibility(View.VISIBLE);
		btnSearch.setVisibility(View.VISIBLE);
		btnSetting.setVisibility(View.GONE);
		showLeft.setVisibility(View.GONE);
		tvStatus.setVisibility(View.GONE);

	}

	private class MySearchTaskOrderAndPage extends
			AsyncTask<String, Integer, String> {
		// onPreExecute方法用于在执行后台任务前做一些UI操作
		TextView tv = (TextView) loadingView.findViewById(R.id.foot_view);

		@Override
		protected void onPreExecute() {
			imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
			tv.setText("请稍后，正在问您加载");
		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@Override
		protected String doInBackground(String... params) {
			// playAnimation();
			getSearchVideoListOrderAndPage(params[0], params[1], params[2]);
			return params[2];
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			tvStatus.setVisibility(View.INVISIBLE);
			lvVideo.setVisibility(View.VISIBLE);
			tv.setText("点击加载更多");
			if (Integer.parseInt(result) >= pages) {
				lvVideo.removeFooterView(loadingView);
			}
			adapter.notifyDataSetChanged();
			lvVideo.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					if (arg2 < videoList.getVideoList().size()) {
						if (!"".equals(videoList.getVideoList().get(arg2)
								.getvLink())) {
							Intent intent = new Intent();
							intent.setClass(getActivity(),
									VideoDetailActivity.class);
							intent.putExtra("url", videoList.getVideoList()
									.get(arg2).getvLink());
							intent.putExtra("img", videoList.getVideoList()
									.get(arg2).getvImg());
							intent.putExtra("title", videoList.getVideoList()
									.get(arg2).getvTitle());
							startActivity(intent);
						} else {
							MyToast toast = new MyToast(getActivity(),
									"该视频无法为您播放");
							toast.show();
						}
					} else {
						new MySearchTaskOrderAndPage().execute(etSearchText,
								"1", currentPage + 1 + "");
						currentPage++;
					}
				}
			});
		}
	}

	// 异步加载
	private class MySearchTask extends AsyncTask<String, Integer, String> {
		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			currentPage = 1;
			// tvStatus.setVisibility(View.VISIBLE);
			lvVideo.setVisibility(View.INVISIBLE);
			imm.hideSoftInputFromWindow(etSearch.getWindowToken(), 0);
			// tvStatus.setText("loading...");
			// lvAdapter = new YoukuVideoListAdapter(getActivity(), videoList,
			// lvVideo);
			// lvVideo.setAdapter(null);
		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@Override
		protected String doInBackground(String... params) {
			// playAnimation();
			return getSearchVideoList(params[0]);
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			tvStatus.setVisibility(View.INVISIBLE);
			lvVideo.setVisibility(View.VISIBLE);
			loadingView = (LinearLayout) LayoutInflater.from(getActivity())
					.inflate(R.layout.foot_list_item, null);
			adapter = new BookItemAdapter(getActivity(), lvVideo,
					videoList.getVideoList());
			if (lvVideo.getFooterViewsCount() < 1) {
				if (isSearch) {
					lvVideo.addFooterView(loadingView);
				}
			}
			lvVideo.setAdapter(adapter);
			adapter.notifyDataSetChanged();
			lvVideo.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					if (arg2 < videoList.getVideoList().size()) {
						if (!"".equals(videoList.getVideoList().get(arg2)
								.getvLink())) {
							Intent intent = new Intent();
							intent.setClass(getActivity(),
									VideoDetailActivity.class);
							intent.putExtra("url", videoList.getVideoList()
									.get(arg2).getvLink());
							intent.putExtra("img", videoList.getVideoList()
									.get(arg2).getvImg());
							intent.putExtra("title", videoList.getVideoList()
									.get(arg2).getvTitle());
							startActivity(intent);
						} else {
							MyToast toast = new MyToast(getActivity(),
									"该视频无法为您播放");
							toast.show();
						}
					} else {
						new MySearchTaskOrderAndPage().execute(etSearchText,
								"1", currentPage + 1 + "");
						currentPage++;
					}
				}
			});
		}

	}

	private int getListSize(Elements content) {
		int i = 0;
		for (Element link : content) {
			// Log.v("href",link.children().select("a")+"");
			if (link.children().select("a").hasAttr("onclick")) {

				i = link.select("a").size();
				Log.v("html", link.select("a") + "");
				Log.v("length", i + "");
			}
		}
		Log.v("size", i + "");
		return i;
	}

}
